package com.ftg.learn.chapter14;

import javax.swing.*;
import java.util.HashSet;

/**
 * @author kangjx
 * @createTime 2022/8/17
 */
public class RectObject {

    private int x;
    private int y;

    RectObject(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public static void main(String[] args) {
        HashSet<RectObject> set = new HashSet<RectObject>();
        RectObject r1 = new RectObject(3, 3);
        RectObject r2 = new RectObject(5, 5);
        RectObject r3 = new RectObject(3, 6);
        set.add(r1);
        set.add(r2);
        set.add(r3);

        //3
        System.out.println(r1.hashCode());
        System.out.println(r2.hashCode());
        System.out.println(r3.hashCode());
        r3.y = 7;

        System.out.println("删除前的大小size:" + set.size());
        set.remove(r3);
        System.out.println(r3.hashCode());
        System.out.println("删除后的大小size:" + set.size());
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + x;
        result = prime * result + y;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final RectObject other = (RectObject) obj;
        if (x != other.x) {
            return false;
        }
        if (y != other.y) {
            return false;
        }
        return true;
    }

}
